#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main() {
    pid_t pid = fork();

    if (pid == 0)
    {
        // 子进程
        printf("Child process is running\n");
        sleep(2);
        printf("Child process is done\n");
        exit(42);  // 子进程退出状态为42
    }
    else if (pid > 0)
    {
        // 父进程
        int status;
        pid_t child_pid = waitpid(pid, &status, 0);
        if (child_pid > 0)
        {
            if (WIFEXITED(status))
            {
                printf("Child process exited with status: %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("Child process did not exit normally\n");
            }
        }
        else
        {
            perror("waitpid");
            exit(EXIT_FAILURE);
        }

    }
    else
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    return 0;
}
